package com.example.pc.myapplication_yjj;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pc.myapplication_yjj.model.ResultObj;
import com.example.pc.myapplication_yjj.utils.HttpUtils;
import com.google.gson.Gson;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_phone;
    private EditText et_psw;
    private TextView tv_commit;
    private AlertDialog alertDialog;
    private CheckBox cb_agree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        cb_agree = findViewById(R.id.cb_agree);
        cb_agree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                tv_commit.setEnabled(isChecked);
            }
        });

        et_phone = findViewById(R.id.et_phone);
        et_psw = findViewById(R.id.et_psw);
        tv_commit = findViewById(R.id.tv_commit);
        tv_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_commit:
                String username = et_phone.getText().toString();
                String psw = et_psw.getText().toString();
                String address = "http://192.168.3.159:8080/home/register.do";
                HashMap<String, String> map = new HashMap<>();
                map.put("userName", username);
                map.put("psw", psw);
                HttpUtils.post(address, map, new HttpUtils.OnHttpCallback() {
                    @Override
                    public void onSuccess(String response) {
                        h.sendMessage(h.obtainMessage(1, response));
                    }

                    @Override
                    public void onFailure(String error) {
                        h.sendMessage(h.obtainMessage(2, error));
                    }
                });
                break;
        }
    }

    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Object obj = msg.obj;
            switch (msg.what) {
                case 1:
                    ResultObj resultObj = new Gson().fromJson(obj.toString(), ResultObj.class);
                    if ("9999".equals(resultObj.getCode())) {
                        Toast.makeText(RegisterActivity.this, resultObj.getMsg(), Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        showAlertDialog(resultObj.getMsg());
                    }
                    break;
                case 2:
                    Toast.makeText(RegisterActivity.this, "error:" + obj, Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });


    private void showAlertDialog(String message) {
        alertDialog = new AlertDialog.Builder(this).setTitle("警告")
                .setMessage(message).setNegativeButton("确认", null).show();

    }


    public void sb(View view) {
        finish();
    }
}
